
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.DESUtil;
import com.kugou.game.sdk.utils.ProtocolCmd;

import org.json.JSONArray;
import org.json.JSONObject;

import android.text.TextUtils;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 描述:获取找回密码渠道信息
 *
 * @author wcj
 * @since 2015年11月5日 下午3:08:12
 */
public class RequestGetPasswdMethodTask {

    private ArrayList<String> mCmdList = new ArrayList<String>();

    public RequestGetpasswdMethodResponse request(String userName) {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();

        HttpBaseRequestPackage req = new HttpBaseRequestPackage();
        req.setUserName(userName);
        RequestGetpasswdMethodResponse respObj = new RequestGetpasswdMethodResponse();
        ResponsePackage resp = new GetPasswdMethodResponsePackage();
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("cmd", ProtocolCmd.CMD_GET_PASSWD_METHOD);
        params.put("username", userName);
        requestParams.add(params);
        req.setRequestParams(requestParams);
        mCmdList.add(ProtocolCmd.CMD_GET_PASSWD_METHOD);
        try {
            KGHttpClient.request(req, resp);
            resp.getResponseData(respObj);
            return respObj;
        } catch (Exception e) {
        }
        return null;
    }

    class GetPasswdMethodResponsePackage extends
            HttpBaseResponsePackage<RequestGetpasswdMethodResponse> {

        @Override
        protected void handleResponse(RequestGetpasswdMethodResponse resetPasswordResponse,
                String jsonStr) {
            try {
                LogUtil.d("response", "RequestGetPasswdMethodTask = " + jsonStr);

                JSONArray responseArray = new JSONArray(jsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    if (i >= mCmdList.size()) {
                        break;
                    }
                    JSONObject jsonResp = responseArray.getJSONObject(i);

                    String cmd = jsonResp.optString("cmd");
                    String code = jsonResp.getString("code");
                    String message = jsonResp.getString("message");
                    String prompt = jsonResp.optString("prompt");
                    resetPasswordResponse.setCode(code);
                    resetPasswordResponse.setMessage(message);
                    resetPasswordResponse.setPrompt(prompt);

                    if (resetPasswordResponse.isUseful()
                            && ProtocolCmd.CMD_GET_PASSWD_METHOD.equals(cmd)) {
                        JSONObject data = jsonResp.optJSONObject("data");
                        String email = data.getString("security_email");
                        String mobile = data.getString("login_mobile");
                        int questionId = 255;

                        if (!TextUtils.isEmpty(email)) {
                            email = DESUtil.decrypt(email, "$1T8fElk");
                        }

                        if (!TextUtils.isEmpty(mobile)) {
                            mobile = DESUtil.decrypt(mobile, "$1T8fElk");
                        }

                        if (!TextUtils.isEmpty(data.getString("question_id"))) {
                            questionId = Integer.parseInt(DESUtil.decrypt(
                                    data.getString("question_id"), "$1T8fElk"));
                        }

                        resetPasswordResponse.setEmail(email);
                        resetPasswordResponse.setMobile(mobile);
                        resetPasswordResponse.setQuestionId(questionId);

                    }
                }

            } catch (Exception e) {
            }
        }

    }

    /**
     * 响应消息
     */
    public static class RequestGetpasswdMethodResponse extends ResponseMessage {

        private String email;

        private String mobile;

        private int questionId = 255;

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        public String getMobile() {
            return mobile;
        }

        public void setMobile(String mobile) {
            this.mobile = mobile;
        }

        public int getQuestionId() {
            return questionId;
        }

        public void setQuestionId(int questionId) {
            this.questionId = questionId;
        }

    }
}
